<?php
// +----------------------------------------------------------------------
// | Calendar(日历
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: CalendarWidget.class.php 104 2011-04-29 03:45:27Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class CalendarWidget extends Widget {

	/**
	 * 构造日历
	 * 
	 */
	public function render($data) {
	
		$timestamp = time();
		$path  =  ROOT_PATH . STATIC_CACHES_DIR;
		$cal   =  $path.'calendar_static.php';
		import("@.ORG.Calendar");
		$y = Calendar::local_date('Y');
		$m = Calendar::local_date('m');	
		$expiration = 0;
		if (($m != local_date('m')) || ($y != local_date('Y')) || (!@include($cal)) || $expiration < $timestamp) {	
			$date = new Calendar($y,$m);
			$start_time = local_mktime(0,0,0,$m,1,$y);
			$end_time   = local_mktime(0,0,0,$date->nextMonth,1,$date->nextYear);
			$num = get_posts_from_date($start_time,$end_time);
			$html = $date->outPut($num);	
			if ($y.$m == local_date('Ym')) {
				if($fp = @fopen($cal, 'wb')) {
					$cachedata = "\$html = unserialize('".addcslashes(serialize($html), '\\\'')."');";
					@fwrite($fp, "<?php\n//nblog cache file\n//Created on " . date('Y-m-d H:i:s',time()) . "\n\nif (!defined('IN_BLOG')) exit('Hacking attempt');\n\n\$expiration='".(time() + 600)."';\r\n".$cachedata."\r\n?>");
					@fclose($fp);
					@chmod($cal, 0777);
				} else {
					$this->error('错误');
				}
			}
		}
		
	    $output = '';
		$output .= '<!-- Calendar: START -->'."\n";
		$output .= " <li class=\"widget widget_calendar dbx-box\">\n";
		$output .= "  <h3 class=\"dbx-handle\">".$html['now_date']."</h3>\n";
		$output .= "   <div id=\"calendar_wrap\" class=\"dbx-content\">\n";
		$output .= "    <table id=\"wp-calendar\" summary=\"Calendar\">\n";
		$output .= "     <thead>\n";
		$output .= "      <tr>\n";
		$output .= "       <th scope=\"col\" title=\"Sunday\">S</th>\n";
		$output .= "       <th scope=\"col\" title=\"Monday\">M</th>\n";
		$output .= "       <th scope=\"col\" title=\"Tuesday\">T</th>\n";
		$output .= "       <th scope=\"col\" title=\"Wednesday\">W</th>\n";
		$output .= "       <th scope=\"col\" title=\"Thursday\">T</th>\n";
		$output .= "       <th scope=\"col\" title=\"Friday\">F</th>\n";
		$output .= "       <th scope=\"col\" title=\"Saturday\">S</th>\n";
		$output .= "      </tr>\n";
		$output .= "     </thead>\n";
		$output .= "     <tfoot>\n";
		$output .= "      <tr>\n";
		$output .= "      <td colspan=\"3\" id=\"prev\"><a href=\"".__APP__."/Date/".$html['prev']."\" title=\"查看 ".$html['prevyear'].$html['prevmonth']." 的日志\">&laquo; ".$html['toprev']."</a></td>\n";
		$output .= "      <td class=\"pad\">&nbsp;</td>\n";
		$output .= "      <td colspan=\"3\" id=\"next\"><a href=\"".__APP__."/Date/".$html['prev']."\" title=\"查看 ".$html['nextyear'].$html['nextmonth']." 的日志\">".$html['tonext']." &raquo;</a></td>\n";
		$output .= "      </tr>\n";
		$output .= "     </tfoot>\n";
		$output .= "     <tbody>\n";
		$output .=       $html['html'];
		$output .= "     </tbody>\n";
		$output .= "    </table>\n";
		$output .= "   </div>\n";
		$output .= " </li>\n";
		$output .= ' <!-- Calendar: END -->'."\n";
		
		return $output;
	}
}

?>
